/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Command;
import com.bourre.data.collections.IndexedArray;
import com.bourre.mvc.AbstractController;

import fever.data.file.File;
import fever.data.file.FileFilter;
import fever.Fever;
import fever.utils.ArrayUtil;
import fever.utils.Stringifier;

import fvaswing.components.filechooser.factory.FvChooserStrategy;
import fvaswing.components.filechooser.factory.FvSOStrategy;
import fvaswing.components.filechooser.mvc.FvFileChooserModel;
import fvaswing.components.filechooser.mvc.FvFileChooserView;
import fvaswing.components.FvFileChooser;

/**
 * <strong>FvFileChooserController</strong> is the <strong>controller</strong> 
 * part of the <strong>MVC Design pattern</strong> implemented in 
 * {@link fvaswing.components.FvFileChooser}.
 *
 * @author Romain Ecarnot
 */
class fvaswing.components.filechooser.mvc.FvFileChooserController 
	extends AbstractController
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _owner : FvFileChooser;
	 	private var _multipleSelection : Boolean;	private var _previewEnabled : Boolean;	private var _uploadEnabled : Boolean;
	private var _filter : IndexedArray;
	private var _acceptAllFilter : Boolean;
	private var _currentFilter : FileFilter;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function FvFileChooserController( owner : FvFileChooser )
	{
		super();
		
		_owner = owner;
		
		_multipleSelection = false;
		_previewEnabled = false;
		_uploadEnabled = false;
		
		_acceptAllFilter = true;
		_initDefaultFileFilter();
		
		setModel( new FvFileChooserModel() );
		setView( new FvFileChooserView( FvFileChooserModel( getModel() ), this ) );
		
		setChooserStrategy( new FvSOStrategy() );
	}
	
	/**
	 * Returns controller's associated model.
	 * 
	 * @return {@link IModel} instance 
	 */
	public function getModel() : FvFileChooserModel
	{
		return FvFileChooserModel( _oModel );
	}
	
	/**
	 * Show a '<strong>custom</strong>' modal dialog containing the file chooser. 
	 * @param buttonCaption Caption of the 'ok' button
	 */
	public function showDialog( id : Number, buttonCaption : String ) : Void
	{
		var ui : FvFileChooserView = FvFileChooserView( getView() );
		
		if( !buttonCaption ) buttonCaption = 'Open';
		
		ui.setType( id, _filter, _getAvailableFilter(), buttonCaption );
		ui.show();
	}
	
	/**
	 * Defines strategy to use in Chooser dialog.
	 */
	public function setChooserStrategy( strategy : FvChooserStrategy ) : Void
	{
		getModel().changeStrategy( strategy );
	}
	
	/**
	 * Sets whether multiple files can be selected at once. 
	 * 
	 * <p>By default, a user can choose only one file.
	 */
	public function setMultiSelectionEnabled( enabled : Boolean ) : Void
	{
		_multipleSelection = enabled;	
	}
	
	/**
	 * Returns whether multiple files can be selected at once. 
	 */
	public function isMultiSelectionEnabled() : Boolean
	{
		return _multipleSelection;
	}
	
	/**
	 * Sets whether upload feature is enabled {@code true} or not 
	 * {@code false}
	 * 
	 * <p>Default is {@code false}
	 */
	public function setUploadEnabled( enabled : Boolean ) : Void
	{
		_uploadEnabled = enabled;
	}
	
	/**
	 * Returns {@code true} if upload feature is enabled in current 
	 * {@link FvFileChooser} component.
	 */
	public function isUploadEnabled() : Boolean
	{
		return _uploadEnabled;	
	}
	
	/**
	 * Sets whether preview feature is enabled {@code true} or not 
	 * {@code false}
	 * 
	 * <p>Default is {@code false}
	 */
	public function setPreviewEnabled( enabled : Boolean ) : Void
	{
		_previewEnabled = enabled;	
	}
	
	/**
	 * Returns {@code true} if preview feature is enabled in current 
	 * {@link FvFileChooser} component.
	 */
	public function isPreviewEnabled( ) : Boolean
	{
		return _previewEnabled;	
	}
	
	/**
	 * Adds type to preview loader.
	 */
	public function addPreviewCommand( type : String, previewCommand : Command ) : Void
	{
		FvFileChooserView( getView() ).addPreviewCommand( type, previewCommand );
	}
	
	/**
	 * Sets the directory whose files are displayed in the file chooser's list.
	 */
	public function setCurrentDirectory( file : File ) : Void
	{
		lockUI();
		getModel().getList( file.getName() );
	}
	
	/**
	 * Retreives directory listing from model cache.
	 * 
	 * <p>This method is called by the UI Combobox.
	 */
	public function retreiveDirectory( path : String ) : Void
	{
		lockUI();
		getModel().retreiveList( path );	
	}
	
	/**
	 * Change the list to display the current directory's parent.
	 */
	public function changeToParentDirectory() : Void
	{
		lockUI();
		getModel().getParent( );
	}
	
	/**
	 * Change the list to display to home directory.
	 */
	public function changeToHomeDirectory() : Void
	{
		lockUI();
		getModel().getHome( );
	}
	
	/**
	 * Check the file system and update the chooser's list.
	 */
	public function rescanCurrentDirectory( path : String ) : Void
	{
		lockUI();
		getModel().rescan( path );
	}
	
	/**
	 * Sets whether the AcceptAll file filter is used as an allowable choice in 
	 * the choosable filter list; the default value is {@code true}.
	 */
	public function setAcceptAllFileFilterUsed( enabled : Boolean ) : Void
	{
		if( enabled != _acceptAllFilter )
		{
			_acceptAllFilter = enabled;
			if( _acceptAllFilter ) _filter.push( FileFilter.getAllFilesFilter() );
			else _filter.remove( FileFilter.getAllFilesFilter() );
		}
	}
	
	/**
	 * Returns whether the AcceptAll file filter is used as an allowable choice in 
	 * the choosable filter list.
	 */
	public function isAcceptAllFileFilterUsed() : Boolean
	{
		return _acceptAllFilter;
	}
	
	/**
	 * Sets the file chooser's primary file filter.
	 */
	public function setFileFilter( filter : FileFilter ) : Void
	{
		if( !ArrayUtil.contains( filter, _filter ) )
		{
			_filter.push( filter );	
		}
		_currentFilter = filter;
	}
	
	/**
	 * Returns the file chooser's primary file filter.
	 */
	public function getFileFilter() : FileFilter
	{
		return _currentFilter;	
	}
	
	/**
	 * Returns file chooser filter list.
	 */
	public function getChoosableFileFilters() : IndexedArray
	{
		return _filter;
	}
	
	/**
	 * Adds a new custom file filter.
	 */
	public function addChoosableFileFilter( filter : FileFilter ) : Void
	{
		_filter.push( filter );	
	}
	
	/**
	 * Removes passed-in {@link fvaswing.components.filechooser.FvFileFilter} from 
	 * file chooser filter list.
	 * 
	 * @return {@code true} is filter is removed, otherwise {@code false}
	 */
	public function removeChoosableFileFilter( filter : FileFilter ) : Boolean
	{
		return _filter.remove( filter );
	}
	
	/**
	 * Resets file chooser filter list to primary filter list.
	 */
	public function resetChoosableFileFilters() : Void
	{
		_initDefaultFileFilter();	
	}
	
	/**
	 * Returns the default 'AcceptAll' filter instance.
	 */
	public function getAcceptAllFileFilter() : FileFilter
	{
		return FileFilter.getAllFilesFilter();
	}
	
	/**
	 * Locks the chooser UI during remoting process.
	 */
	public function lockUI() : Void
	{
		Fever.application.protect();
	}
	
	/**
	 * Unlocks the chooser UI.
	 */
	public function unlockUI() : Void
	{
		Fever.application.unprotect();
	}
	
	/**
	 * Triggered by user when the chooser is closed.
	 * 
	 * @param type  Possible values are 
	 * <ul>
	 *   <li>{@link #fvaswing.components.FvFileChooser.APPROUVE_OPTION}</li>
	 *   <li>{@link #fvaswing.components.FvFileChooser.CANCEL_OPTION}</li>
	 *   <li>{@link #fvaswing.components.FvFileChooser.ERROR_OPTION}</li>
	 * </ul>
	 * @param files Array structure with all selected files
	 * @param path Base path of selected files
	 */
	public function handleResultEvent( type : Number, files : Array, path : String ) : Void
	{
		_owner.fireResult( type, files, path );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _initDefaultFileFilter() : Void
	{
		_filter = new IndexedArray();
		
		if( isAcceptAllFileFilterUsed() ) _filter.push( FileFilter.getAllFilesFilter() );
	}
	
	private function _getAvailableFilter() : FileFilter
	{
		if( _currentFilter ) return _currentFilter;
		if( isAcceptAllFileFilterUsed() ) return FileFilter.getAllFilesFilter();
		
		return _filter[0];
	}
}